package models
{
	import air.update.ApplicationUpdaterUI;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	
	import mx.collections.XMLListCollection;
	
	import views.WaitWindow;
	
	[Bindable]
	public class ConfigModel
	{
		public function ConfigModel()
		{
		}
		public const connectUrl:String = "rtmfp://stratus.adobe.com";
		public const developerKey:String = "39eb05e76eabb57deb96b56a-937bc372b0a4";
		public const baseURL:String = "app:/games/";
		public const configURL:String = "http://p2pgames.googlecode.com/svn/trunk/P2PGameLobby/src/config.xml";
		public const appDesc:XML = NativeApplication.nativeApplication.applicationDescriptor;
		public const appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
		public var statusBarText:String;
		public var gameList:XMLListCollection;
		public function getVersion():String{
			return appDesc.children()[3];
		}
		public function loadConfig():void{
			WaitWindow.wait("加载配置文件");
			new URLLoader(new URLRequest(configURL)).addEventListener(Event.COMPLETE,onConfigLoaded,false,0,true);
		}
		private function onConfigLoaded(event:Event):void{
			var config:XML = new XML(event.target.data);
			gameList = new XMLListCollection(config.gameList[0].children());
			for each(var gameXML:XML in gameList){
				var file:File = File.applicationStorageDirectory.resolvePath(gameXML.@src);
				if(file.exists){
					gameXML.@exists = true;
				}else{
					gameXML.@exists = false;
				}
			}
			new Updater(config.updateURL,getVersion());
			WaitWindow.waitThingDone("加载配置文件");
		}
	}
}
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;

import views.WaitWindow;

class Updater
{
	private var version:String;
	private var file:File = File.applicationStorageDirectory.resolvePath("P2PGameLobby.air");
	private static var instance:Updater;
	public function Updater(updateURL:String,version:String)
	{
		instance = this;
		this.version = version;
		var loadUpdateXML:URLLoader = new URLLoader(new URLRequest(updateURL));
		loadUpdateXML.addEventListener(Event.COMPLETE,onUpdateXMLLoaded,false,0,true);
		WaitWindow.wait("检查更新");
	}
	private function onUpdateXMLLoaded(event:Event):void{
		var string:String = event.target.data;
		var v:String = string.substring(string.indexOf("<version>")+9,string.indexOf("</version>"));
		var fileURL:String = string.substring(string.indexOf("<url>")+5,string.indexOf("</url>"));
		if(version != v){
			version = v;
			WaitWindow.wait("下载更新");
			var newFileLoader:URLStream = new URLStream();
			newFileLoader.addEventListener(Event.COMPLETE, loaded,false,0,true);
			newFileLoader.load(new URLRequest(fileURL));
		}else{
			instance = null;
			file = null;
			sendDexterEvent("configComplete");
		}
		WaitWindow.waitThingDone("检查更新");
	}
	private function loaded(event:Event):void{
		var fileData:ByteArray = new ByteArray();
		var urlStream:URLStream = event.target as URLStream;
		urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
		var fileStream:FileStream = new FileStream();
		var tempFile:File = File.createTempFile();
		fileStream.open(tempFile, FileMode.WRITE);
		fileStream.writeBytes(fileData, 0, fileData.length);
		fileStream.close();
		tempFile.moveTo(file,true);
		new flash.desktop.Updater().update(file,version);
	}
}